// views/index/shop/translationcopy/ordermessage/index.js
var util = require('../../../../../utils/util');
var area = require('../../../../../utils/area');

var BASE_URL = getApp().globalData.BASE_URL
import Notify from '../../../../../miniprogram_npm/@vant/weapp/notify/notify';
import Dialog from '../../../../../miniprogram_npm/@vant/weapp/dialog/dialog';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: '',
    areaList: area.default,
    minDate: new Date(1950, 0, 1).getTime(),
    opendiqupop: false,
    showdatepop: false,
    currentDate: new Date().getTime(),
    nowtime: "",
    formatter(type, value) {
      if (type === 'year') {
        return `${value}年`;
      }
      if (type === 'month') {
        return `${value}月`;
      }
      return value;
    },
    user_name: '',
    xing: "",
    ming: '',
    sex: 1,
    born: "yyyy/mm/日",
    area: "北京市",
    apply: '未婚',
    guoji: '中国',
    huzhaohaoma: '',
    IBAN: '',
    BIC: '',
    kaihuname: '',

    images: '',
    saomiao: "",
    lutong: '',
    zhengshu: '',

    images_src: '',
    saomiao_src: "",
    lutong_src: '',
    zhengshu_src: '',




    ifswitch: false
  },



  //立即办理
  placeorder: function () {
    var _this = this
    var userinfo = wx.getStorageSync("USER")
    if (!_this.ifhanzi(_this.data.user_name)) {
      Notify('姓名输入错误');
    }
    if (!_this.ifenglish(_this.data.xing)) {
      Notify('姓拼音输入错误');
    }
    if (!_this.ifenglish(_this.data.ming)) {
      Notify('名拼音输入错误');
    }
    if (_this.data.huzhaohaoma == "") {
      Notify('护照号码不能为空');
    }
    if (_this.data.ifswitch) {
      if (_this.data.IBAN == "") {
        Notify('银行账户IBAN不能为空');
      }
      if (_this.data.BIC == "") {
        Notify('银行账户BIC不能为空');
      }
      if (_this.data.kaihuname == "") {
        Notify('开户人名字不能为空');
      }

      if (_this.ifhanzi(_this.data.user_name) && _this.ifenglish(_this.data.xing) && _this.ifenglish(_this.data.ming) && _this.data.huzhaohaoma !== "" && _this.data.IBAN !== "" && _this.data.BIC !== "" && _this.data.kaihuname !== "") {
        wx.request({
          url: `${BASE_URL}/baoxian/gonglidata`,
          method: "POST",
          data: {
            uid: userinfo.id,
            type: 'xiaochengxv',
            name: _this.data.user_name,
            x_spell: _this.data.xing,
            m_spell: _this.data.ming,
            sex: _this.data.sex,
            id:_this.data.id,
            birth: _this.data.born,
            born: _this.data.area,
            marriage: _this.data.apply,
            country: _this.data.guoji,
            passport: _this.data.huzhaohaoma,
            or_bank: _this.data.ifswitch ? 1 : 0,
            bank_hu: _this.data.IBAN,
            bank_bic: _this.data.BIC,
            bank_ren: _this.data.kaihuname,
          },
          header: {
            'Content-Type': 'application/json' //GET方式是这个
          },
          success(res) {
            console.log(res)
            wx.navigateTo({
              url: `./second/index?id=${res.data.id}`
            })
          }
        })
      }
    } else {
      if (_this.ifhanzi(_this.data.user_name) && _this.ifenglish(_this.data.xing) && _this.ifenglish(_this.data.ming) && _this.data.huzhaohaoma !== "") {
        wx.request({
          url: `${BASE_URL}/baoxian/gonglidata`,
          method: "POST",
          data: {
            uid: userinfo.id,
            type: 'xiaochengxv',
            name: _this.data.user_name,
            x_spell: _this.data.xing,
            m_spell: _this.data.ming,
            sex: _this.data.sex,
            birth: _this.data.born,
            born: _this.data.area,
            marriage: _this.data.apply,
            id:_this.data.id,
            country: _this.data.guoji,
            passport: _this.data.huzhaohaoma,
            or_bank: _this.data.ifswitch ? 1 : 0,
            bank_hu: _this.data.IBAN,
            bank_bic: _this.data.BIC,
            bank_ren: _this.data.kaihuname,
          },
          header: {
            'Content-Type': 'application/json' //GET方式是这个
          },
          success(res) {
            console.log(res)
            wx.navigateTo({
              url: `./second/index?id=${res.data.id}`
            })
          }
        })

      }

    }

  },




  //上传文件
  upfile: function (e) {
    console.log(e.currentTarget.dataset.type)
    var _this = this
    var userinfo = wx.getStorageSync("USER")
    wx.chooseMessageFile({
      count: 1,
      success(res) {
        const tempFilePaths = res.tempFiles
        console.log(res, tempFilePaths)
        if (e.currentTarget.dataset.type == 'images') {
          _this.setData({
            images: tempFilePaths[0].name,
            images_src: tempFilePaths[0].path
          })
        }
        if (e.currentTarget.dataset.type == 'saomiao') {
          _this.setData({
            saomiao: tempFilePaths[0].name,
            saomiao_src: tempFilePaths[0].path

          })
        }
        if (e.currentTarget.dataset.type == 'lutong') {
          _this.setData({
            lutong: tempFilePaths[0].name,
            lutong_src: tempFilePaths[0].path
          })
        }
        if (e.currentTarget.dataset.type == 'zhengshu') {
          _this.setData({
            zhengshu: tempFilePaths[0].name,
            zhengshu_src: tempFilePaths[0].path
          })
        }



        // for (var i = 0; i < tempFilePaths.length; i++) { // }
        wx.uploadFile({
          url: `${BASE_URL}/baoxian/baoxianwenjianup`, //仅为示例，非真实的接口地址
          filePath: tempFilePaths[0].path,
          name: 'file',
          header: {
            "Content-Type": "multipart/form-data"
          },
          formData: {
            type:e.currentTarget.dataset.type,
            id: _this.data.id,
          },
          success(res) {
            console.log(res)
            if (res.statusCode == 200) {
              wx.hideLoading()
              wx.showToast({
                title: "上传成功",
                icon: "none",
                duration: 1500
              })
            }
            var data = JSON.parse(res.data)
            console.log(data)
            if (data.status == 4) {
              wx.showToast({
                title: `上传失败,${data.error}`,
                icon: "none",
                duration: 2000
              })
            }
          }
        })

      }
    })
  },






  getdatalist: function () {
    var _this = this
    var userinfo = wx.getStorageSync("USER")
    wx.request({
      url: `${BASE_URL}/baoxian/gonglidata`, //仅为示例，并非真实的接口地址
      data: {
        id: _this.data.id,
        type: 'xiaochengxv',
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      method: "get",
      success(res) {
        console.log(res)
        if(res.data.list.info){
          res.data.list.info = JSON.parse(res.data.list.info)
        }
        if(res.data.list.xinxi){
          res.data.list.xinxi = JSON.parse(res.data.list.xinxi)
        }
        _this.setData({
          list: res.data,
          // user_name: res.data.list.name,
          // xing: res.data.list.x_spell,
          // ming: res.data.list.m_spell,
          // sex: res.data.list.sex,
          // born: res.data.list.birth,
          // area: res.data.list.born,
          // apply: res.data.list.marriage,
          // huzhaohaoma: res.data.list.passport,
          // images: res.data.con.images.name,
          // saomiao: res.data.con.saomiao.name,
          // lutong: res.data.con.lutong.name,
          // zhengshu: res.data.con.zhengshu.name,
        })
      }
    })
  },













  change_switch: function (e) {
    console.log(e)
    this.setData({
      ifswitch: e.detail
    })
  },



  //确定时间
  change_date: function () {
    this.setData({
      showdatepop: false,
      born: this.changeTime(this.data.nowtime)
    })
  },
  change_jiudu: function () {
    this.setData({
      openjiudupop: false,
      du_time: this.changeTime(this.data.nowtime)
    })
  },
  //切换时间
  getColumnValue(event) {
    console.log(event)
    this.setData({
      nowtime: event.detail,
    });
  },

  //打开时间pop
  opendatepop: function () {
    this.setData({
      showdatepop: true
    })
  },
  opendiqupop: function () {
    this.setData({
      opendiqupop: true
    })
  },
  //关闭时间pop
  onClose: function () {
    this.setData({
      showdatepop: false,
      opendiqupop: false
    })
  },
  //打开地区选择遮罩层
  open_area: function (e) {
    this.setData({
      opendiqupop: true
    })
  },
  //选择好地区并关闭地区选择器遮罩呈
  changeuserarea: function (e) {
    console.log(e)
    var str = []
    e.detail.values.forEach((item, index) => {
      if (item) {
        str.push(item.name)
      }
    })
    this.setData({
      opendiqupop: false,
      area: str
    })
  },
  //关闭遮罩
  closeuserarea: function () {
    this.setData({
      opendiqupop: false
    })
  },
  //领域切换
  change_apply: function (e) {
    this.setData({
      apply: e.currentTarget.dataset.apply
    })
  },
  //男女切换
  change_sex: function (e) {
    this.setData({
      sex: e.currentTarget.dataset.sex
    })
  },















  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      id:options.id
      // id: 1427
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.getdatalist()

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var userinfo = wx.getStorageSync("USER")
    this.setData({
      email: userinfo.email
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  //手机号正则
  codeVerification: function (phone) {
    let phoneCodeVerification = /^[1][3,4,5,7,8][0-9]{9}$/;
    return phoneCodeVerification.test(phone);
  },
  //英文字母正则
  ifenglish: function (pinyin) {
    let ifenglish = /^[A-Za-z]+$/;
    return ifenglish.test(pinyin);
  },
  //汉字正则
  ifhanzi: function (pinyin) {
    let ifhanzi = /^[\u4e00-\u9fa5]+$/;
    return ifhanzi.test(pinyin);
  },
  //微信号正则
  ifwewhat: function (pinyin) {
    let ifwewhat = /^[0-9a-zA-Z]*$/g;
    return ifwewhat.test(pinyin);
  },
  //邮箱验证
  ifemail: function (email) {
    let ifemail = /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/;
    return ifemail.test(email);
  },









  // 时间戳转换
  changeTime(time) {
    if (time) {
      var oDate = new Date(time * 1),
        oYear = oDate.getFullYear(),
        oMonth = oDate.getMonth() + 1,
        oDay = oDate.getDate(),
        oTime = oYear + "/" + this.getBz(oMonth) + "/" + this.getBz(oDay)
      return oTime;
    } else {
      return "";
    }
  },
  // 补0
  getBz(num) {
    if (parseInt(num) < 10) {
      num = "0" + num;
    }
    return num;
  },


})